package com.todo.strategy.online.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.api.client.util.Lists;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.online.infrastructure.entity.domain.PlatformStrategyDO;
import com.todo.strategy.online.infrastructure.entity.po.PlatformStrategyDetailPO;
import com.todo.strategy.online.infrastructure.entity.po.PlatformStrategyPO;
import com.todo.strategy.online.infrastructure.mapper.PlatformStrategyDetailMapper;
import com.todo.strategy.online.infrastructure.mapper.PlatformStrategyMapper;
import com.todo.strategy.online.infrastructure.repository.PlatformStrategyRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (PlatformStrategy)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("platformStrategyRepository")
public class PlatformStrategyRepositoryImpl implements PlatformStrategyRepository {
    @Resource
    private PlatformStrategyMapper platformStrategyMapper;
    @Resource
    private PlatformStrategyDetailMapper platformStrategyDetailMapper;


    @Override
    public PlatformStrategyDO queryPlatformStrategyById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        PlatformStrategyPO platformStrategyPO = platformStrategyMapper.queryPlatformStrategyPOById(id);
        if (platformStrategyPO == null) {
            return null;
        }
        PlatformStrategyDetailPO platformStrategyDetailPO = platformStrategyDetailMapper.queryPlatformStrategyDetailPOById(id);
        PlatformStrategyDO platformStrategyDO = convertToPlatformStrategyDO(platformStrategyPO, platformStrategyDetailPO);
        return platformStrategyDO;
    }

    @Override
    public PlatformStrategyPO queryBasePlatformStrategyById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        PlatformStrategyPO platformStrategyPO = platformStrategyMapper.queryPlatformStrategyPOById(id);
        return platformStrategyPO;
    }

    @Override
    public PlatformStrategyDetailPO queryPlatformStrategyDetailById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        PlatformStrategyDetailPO platformStrategyDetailPO = platformStrategyDetailMapper.queryPlatformStrategyDetailPOById(id);
        return platformStrategyDetailPO;
    }

    @Override
    public List<PlatformStrategyPO> queryPlatformStrategyByIdList(List<Long> strategyIdList) {
        if (CollectionUtils.isEmpty(strategyIdList)) {
            return Lists.newArrayList();
        }
        return platformStrategyMapper.queryPlatformStrategyByIdList(strategyIdList);
    }

    @Override
    public Boolean updatePlatformStrategyInfo(PlatformStrategyPO platformStrategyPO) {
        if (platformStrategyPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (platformStrategyPO.getTitle() == null || platformStrategyPO.getRecommendDesc() == null || platformStrategyPO.getStrategyType() == null || platformStrategyPO.getTravelTimeType() == null || platformStrategyPO.getStatus() == null) {
            log.warn("PlatformStrategyRepository#updatePlatformStrategyInfo 攻略基础信息字段校验失败，默认操作失败 platformStrategyPO：【{}】", JSONObject.toJSONString(platformStrategyPO));
            return false;
        }
        int strategyResult = platformStrategyMapper.updatePlatformStrategyPOById(platformStrategyPO);
        if (strategyResult == 0) {
            log.warn("PlatformStrategyRepository#updatePlatformStrategyById 更新平台攻略信息失败，攻略信息更新失败 platformStrategyPO：【{}】", JSONObject.toJSONString(platformStrategyPO));
            return false;
        }

        return true;
    }

    @Override
    public Boolean insertPlatformStrategyDetail(PlatformStrategyDO platformStrategyDO) {
        if (platformStrategyDO == null) {
            return false;
        }
        PlatformStrategyPO platformStrategyPO = buildPlatformStrategyPO(platformStrategyDO);
        platformStrategyMapper.insertPlatformStrategyPO(platformStrategyPO);
        PlatformStrategyDetailPO platformStrategyDetailPO = buildPlatformStrategyDetailPO(platformStrategyDO);
        if (platformStrategyDetailPO != null) {
            platformStrategyDetailMapper.insertPlatformStrategyDetailPO(platformStrategyDetailPO);
        }
        return true;
    }

    @Override
    public Boolean updatePStrategyDetail(PlatformStrategyDO platformStrategyDO) {
        if (platformStrategyDO == null) {
            return false;
        }
        PlatformStrategyPO platformStrategyPO = buildPlatformStrategyPO(platformStrategyDO);
        platformStrategyMapper.updatePlatformStrategyPOById(platformStrategyPO);
        PlatformStrategyDetailPO platformStrategyDetailPO = buildPlatformStrategyDetailPO(platformStrategyDO);
        if (platformStrategyDetailPO != null) {
            platformStrategyDetailMapper.updatePlatformStrategyDetailPOById(platformStrategyDetailPO);
        }
        return true;
    }

    @Override
    public Boolean removePStrategyDetail(Long businessId) {
        if (businessId == null){
            return false;
        }
        platformStrategyMapper.removePlatformStrategyPOById(businessId);
        platformStrategyDetailMapper.removePlatformStrategyDetailPOById(businessId);
        return true;
    }

    public PlatformStrategyDO convertToPlatformStrategyDO(PlatformStrategyPO platformStrategyPO, PlatformStrategyDetailPO platformStrategyDetailPO) {
        if (platformStrategyPO == null || platformStrategyDetailPO == null) {
            return null;
        }
        PlatformStrategyDO platformStrategyDO = new PlatformStrategyDO();
        platformStrategyDO.setId(platformStrategyPO.getId());
        platformStrategyDO.setIsDelete(platformStrategyPO.getIsDelete());
        platformStrategyDO.setCreateUser(platformStrategyPO.getCreateUser());
        platformStrategyDO.setLastUpdateUser(platformStrategyPO.getLastUpdateUser());
        platformStrategyDO.setCreateTime(platformStrategyPO.getCreateTime());
        platformStrategyDO.setUpdateTime(platformStrategyPO.getUpdateTime());

        PlatformStrategyDO.StrategyDetail strategyDetail = new PlatformStrategyDO.StrategyDetail();
        strategyDetail.setAgeSuggest(platformStrategyDetailPO.getAgeSuggest());
        strategyDetail.setTravelSuggest(platformStrategyDetailPO.getTravelSuggest());
        strategyDetail.setInstructions(platformStrategyDetailPO.getInstructions());
        strategyDetail.setSecuritySuggest(platformStrategyDetailPO.getSecuritySuggest());
        strategyDetail.setRemark(platformStrategyDetailPO.getRemark());
        strategyDetail.setExtra(platformStrategyDetailPO.getExtra());
        platformStrategyDO.setStrategyDetail(strategyDetail);

        PlatformStrategyDO.BaseStrategyInfo baseStrategyInfo = new PlatformStrategyDO.BaseStrategyInfo();
        baseStrategyInfo.setCity(platformStrategyPO.getCity());
        baseStrategyInfo.setTimeCost(platformStrategyPO.getTimeCost());
        baseStrategyInfo.setTravelTimeType(platformStrategyPO.getTravelTimeType());
        baseStrategyInfo.setIsFree(platformStrategyPO.getIsFree());
        baseStrategyInfo.setMoneyCost(platformStrategyPO.getMoneyCost());
        baseStrategyInfo.setScore(platformStrategyPO.getScore());
        baseStrategyInfo.setStatus(platformStrategyPO.getStatus());
        baseStrategyInfo.setAllowComment(platformStrategyPO.getAllowComment());
        baseStrategyInfo.setReferenceCount(platformStrategyPO.getReferenceCount());
        baseStrategyInfo.setTitle(platformStrategyPO.getTitle());
        baseStrategyInfo.setProvince(platformStrategyPO.getProvince());
        baseStrategyInfo.setRecommendMonth(platformStrategyPO.getRecommendMonth());
        baseStrategyInfo.setMainImg(platformStrategyPO.getMainImg());
        baseStrategyInfo.setRecommendDesc(platformStrategyPO.getRecommendDesc());
        baseStrategyInfo.setStrategyType(platformStrategyPO.getStrategyType());
        platformStrategyDO.setBaseStrategyInfo(baseStrategyInfo);
        return platformStrategyDO;
    }

    public PlatformStrategyPO buildPlatformStrategyPO(PlatformStrategyDO platformStrategyDO) {
        if (platformStrategyDO == null || platformStrategyDO.getBaseStrategyInfo() == null) {
            return null;
        }
        PlatformStrategyPO platformStrategyPO = new PlatformStrategyPO();
        PlatformStrategyDO.BaseStrategyInfo baseStrategyInfo = platformStrategyDO.getBaseStrategyInfo();
        platformStrategyPO.setCity(baseStrategyInfo.getCity());
        platformStrategyPO.setTimeCost(baseStrategyInfo.getTimeCost());
        platformStrategyPO.setTravelTimeType(baseStrategyInfo.getTravelTimeType());
        platformStrategyPO.setIsFree(baseStrategyInfo.getIsFree());
        platformStrategyPO.setMoneyCost(baseStrategyInfo.getMoneyCost());
        platformStrategyPO.setScore(baseStrategyInfo.getScore());
        platformStrategyPO.setStatus(baseStrategyInfo.getStatus());
        platformStrategyPO.setAllowComment(baseStrategyInfo.getAllowComment());
        platformStrategyPO.setReferenceCount(baseStrategyInfo.getReferenceCount());
        platformStrategyPO.setTitle(baseStrategyInfo.getTitle());
        platformStrategyPO.setProvince(baseStrategyInfo.getProvince());
        platformStrategyPO.setRecommendMonth(baseStrategyInfo.getRecommendMonth());
        platformStrategyPO.setMainImg(baseStrategyInfo.getMainImg());
        platformStrategyPO.setRecommendDesc(baseStrategyInfo.getRecommendDesc());
        platformStrategyPO.setStrategyType(baseStrategyInfo.getStrategyType());
        platformStrategyPO.setId(platformStrategyDO.getId());
        platformStrategyPO.setIsDelete(platformStrategyDO.getIsDelete());
        platformStrategyPO.setCreateUser(platformStrategyDO.getCreateUser());
        platformStrategyPO.setLastUpdateUser(platformStrategyDO.getLastUpdateUser());
        platformStrategyPO.setLastUpdateUser(platformStrategyDO.getLastUpdateUser());
        platformStrategyPO.setCreateTime(platformStrategyDO.getCreateTime());
        return platformStrategyPO;
    }

    public PlatformStrategyDetailPO buildPlatformStrategyDetailPO(PlatformStrategyDO platformStrategyDO) {
        if (platformStrategyDO == null || platformStrategyDO.getStrategyDetail() == null) {
            return null;
        }
        PlatformStrategyDetailPO platformStrategyDetailPO = new PlatformStrategyDetailPO();
        PlatformStrategyDO.StrategyDetail strategyDetail = platformStrategyDO.getStrategyDetail();
        platformStrategyDetailPO.setId(platformStrategyDO.getId());
        platformStrategyDetailPO.setAgeSuggest(strategyDetail.getAgeSuggest());
        platformStrategyDetailPO.setTravelSuggest(strategyDetail.getTravelSuggest());
        platformStrategyDetailPO.setInstructions(strategyDetail.getInstructions());
        platformStrategyDetailPO.setSecuritySuggest(strategyDetail.getSecuritySuggest());
        platformStrategyDetailPO.setRemark(strategyDetail.getRemark());
        platformStrategyDetailPO.setExtra(strategyDetail.getExtra());
        platformStrategyDetailPO.setIsDelete(platformStrategyDO.getIsDelete());
        platformStrategyDetailPO.setCreateTime(platformStrategyDO.getCreateTime());
        platformStrategyDetailPO.setUpdateTime(platformStrategyDO.getUpdateTime());
        return platformStrategyDetailPO;
    }

}
